<template>
  <div class="lin-container">
    <el-form label-width="220px" :model="form">
      <el-form-item label="标题">
        <el-input v-model="form.indexName" style="width:240px"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input type="textarea" v-model="form.indexContent" style="width:240px"></el-input>
      </el-form-item>
      <el-form-item label="上传图片" class="uploadimage">
        <upload-imgs
          ref="uploadEle"
          :rules="rules"
          :imageFileList="imageFileList"
          @setForm="getForm"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="reset">取消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import UploadImgs from "@/components/UploadImg/index.vue";
import { menuGetModuleContent, addOrUpdateMenu } from "@/api/PC-Home.js";
export default {
  name: "ImgsUploadStage",
  components: {
    UploadImgs
  },
  data() {
    return {
      form: {
        indexName:"",
        indexContent:""
      },
      formLabelWidth: "140px",
      rules: {
        maxSize: 1,
        minWidth: 100,
        minHeight: 100
      },
      imageFileList:[],
      file:null,
      queryId:null
    };
  },
  mounted() {
    this.getQueryId();
    this.menuGetModuleContent();
  },
  methods: {
    getQueryId() {
      this.queryId = this.$route.query.queryId;
    },
    menuGetModuleContent() {
      this.imageFileList = []
      let param = {
        id: this.queryId
      };
      menuGetModuleContent(param).then(res => {
        this.form.indexName = res.data[0].indexName
        this.form.indexContent = res.data[0].indexContent
        this.form.id = res.data[0].id
        this.imageFileList.push({
          name: res.data[0].indexName,
          url: process.env.VUE_APP_URL + "/" + res.data[0].indexImg
        });
      });
    },
    getForm(value) {
      this.file = value;
    },
    reset(){
      this.menuGetModuleContent();
      this.file = null
    },
    submit() {
      let file = this.file;
      let form = null;
      form = new FormData();
      form.append("file", file?file.raw:'');
      form.append("parentId", this.queryId);
      form.append("indexName", this.form.indexName)
      form.append("indexContent", this.form.indexContent)
      form.append("id", this.form.id ? this.form.id : "")
      addOrUpdateMenu(form).then(res => {
        if (res.code === 200) {
          this.$message.success("上传成功");
          this.menuGetModuleContent();
          this.dialogFormVisible = false;
        } else {
          this.$message.error(res.message);
        }
      });
    },
  }
};
</script>

    
<style scoped lang="scss">
.lin-wrap {
  padding: 40px 0;
  .wrap-label {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    .label {
      font-size: 16px;
    }
  }
}
::v-deep .uploadimage {
  display: flex;
  flex-direction: row;
  .el-form-item__content {
    display: flex;
    flex-direction: column;
    align-items: flex-start;

    .upload-demo {
      display: flex !important;
      flex-direction: row !important;
      align-items: center !important;

      .el-upload {
        margin-right: 10px;
      }
    }
    .demo-image__preview {
      display: flex;
      flex-direction: row;
    }
  }
}
::v-deep .el-dialog__header {
  display: flex;
  .el-dialog__headerbtn {
    top: 0;
  }
}
</style>
